package com.ganee.agora.view.fragment


import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener

import com.ganee.agora.R
import com.ganee.agora.adapter.YourListAdapter
import com.ganee.agora.model.InsertActivityEntity
import com.ganee.agora.viewmodel.HomeViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_home.*

/** Sign-ups
 * A simple [Fragment] subclass.
 * 已报名列表
 */
class RegistrationsFragment : BaseFragment<HomeViewModel>() {

    private var mAdapter: YourListAdapter? = null
    private val mList = mutableListOf<InsertActivityEntity>()

    override fun getViewModel(): HomeViewModel {
        return ViewModelProviders.of(this).get(HomeViewModel::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ARouter.getInstance().inject(this)
        initViewAndAdapter()
        initRequestData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        initRequestData()
    }

    private fun initViewAndAdapter() {
        tvTitle.visibility = View.VISIBLE
        tvTitle.text = "Sign-ups"

        homeRecyclerView.setHasFixedSize(true)
        homeRecyclerView.layoutManager = LinearLayoutManager(activity)

        mAdapter = YourListAdapter(mList)
        homeRecyclerView.addOnItemTouchListener(object : OnItemClickListener() {

            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                val entity= adapter.getItem(position) as InsertActivityEntity
                ARouter.getInstance().build(Constance.ACTIVITY_URL_TICKET_DETAIL).withString("activeId",entity.activeId).navigation()
            }

        })
        homeRecyclerView.adapter = mAdapter
    }


    private fun initRequestData() {
        viewModel.getRegistrationsList()
        viewModel.registration.observe(this, Observer<List<InsertActivityEntity>> { list ->
            mList.clear()
            mList.addAll(list)
            mAdapter!!.notifyDataSetChanged()
        })
    }
}
